console.log("Hello via Bun!");


// const data = `It was the best of times, it was the worst of times.`;
// await Bun.write("test_output.txt", data);

// import { mkdir } from "node:fs/promises";
// await mkdir("test/dir", { recursive: true });

import { readdir } from "node:fs/promises";
import type path from "node:path";
import { Glob } from "bun";

// read all the files in the current directory
// const files = await readdir(import.meta.dir);
const files = new Glob("*");

// for await (const file of files.scan(".")) {
//   console.log(file); // => "index.ts"
// }
const host_path = "C:/Windows/System32/drivers/etc/hosts"
const foo = Bun.file(host_path);

var content = await foo.text(); // contents as a string
// console.log(content)
// await foo.stream(); // contents as ReadableStream
// await foo.arrayBuffer(); // contents as ArrayBuffer
// await foo.bytes(); // contents as Uint8Array

var arr = [
    "127.0.0.1 ad.skyworth.com",
    "127.0.0.1 ad2.skyworth.com",
]

// 按行分割开
var content_lines = content.split("\n")
// console.log(content_lines)

import { appendFile } from "node:fs/promises";
import { appendFileSync } from "node:fs";

async function write_data(data: string, file_path: string) {
    // await Bun.write(path, item_data);
    var data = "\r\n" + data
    // await appendFile(file_path,data)
    appendFileSync(file_path, data);

}

// 先读取
const test_hosts_path = "test_hosts.txt"
await Bun.write(test_hosts_path, "# fanghongjian");
content_lines.forEach(function (item, index, array) {
    write_data(item, test_hosts_path)
})
arr.forEach(function (item, index, array) {
    // console.log(item);
    var result = content_lines.includes(item);
    // console.log(result)
    if (!result) {
        write_data(item, test_hosts_path)
    } else {
        write_data(item, test_hosts_path)
    }
})

console.log('Start');
setTimeout(() => {
    console.log('Executed after 5 seconds');
}, 5000);
console.log('End');